package com.ch999.detect.mode;

import android.content.Context;

import com.ch999.detect.constant.SpType;
import com.ch999.detect.mode.bean.GoodsBjgz;
import com.ch999.detect.mode.bean.RecoverBasketEntity;
import com.ch999.detect.utils.SharePrefUtil;
import com.google.gson.Gson;
import com.scorpio.mylib.http.MyHttp;
import com.scorpio.mylib.http.RequestParams;
import com.scorpio.mylib.http.iface.DataResponse;
import com.scorpio.mylib.http.iface.TextHandler;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import static com.ch999.detect.constant.SpType.KEY_BJGZ;
import static com.ch999.detect.constant.SpType.KEY_PHONE_PID;

/**
 * Created by Administrator on 2016/12/14 0014.
 */

public class DetctControl {

    public static String BASE_URL = "https://huishou.9ji.com/app/";
    public static String JI_XING = "getParms";
    public static String GOODS = "getallgoods";
    public static String GetNewRecoverBasket = "http://huishou.9ji.com/ajaxhandler/GetNewRecoverBasket";
    public static String SAVEATTR = "saveAttr";
    public static String TAG = "DetctControl";


    public interface JiXinDataResponse extends DataResponse {
        public void onEmpty();
    }


    public static void getPhoneData(final Context mContext, String jixing, final JiXinDataResponse dr) {
        RequestParams params = new RequestParams();

        params.put("jixing", jixing);
//        params.put("jixing", "SM-A7000");

        params.put("bname", SharePrefUtil.getInstance(mContext).getString(SpType.KEY_BAND_ID));
        params.put("pid", SharePrefUtil.getInstance(mContext).getString(KEY_PHONE_PID));
//        params.put("bname", 0);
//        params.put("pid", 1);
        params.put("t", System.currentTimeMillis());
        MyHttp.get(BASE_URL + JI_XING, params, new TextHandler() {
            @Override
            public void onSuccess(int header, String response) {
                try {

                    if (response == null) {
                        dr.onEmpty();
                    }

                    JSONObject jsonObject = new JSONObject(response);

                    int stats = jsonObject.getInt("stats");
                    if (stats == 1) {
                        String data = jsonObject.getString("data");

                        Gson gson = new Gson();
                        GoodsBjgz goodsBjgz = gson.fromJson(data, GoodsBjgz.class);
                        dr.onSucc(goodsBjgz);

                        SharePrefUtil.getInstance(mContext).putInt(SpType.KEY_POSATION, -1); //检测位置归0
                        SharePrefUtil.getInstance(mContext).putString(SpType.KEY_PHONE_DATA, data);//保存手机数据
                        SharePrefUtil.getInstance(mContext).putString(KEY_PHONE_PID, goodsBjgz.getPid() + "");
                        SharePrefUtil.getInstance(mContext).putString(KEY_BJGZ, goodsBjgz.getBjgz() + "");

                    } else if (stats == 0) {
                        dr.onEmpty();
                    }


                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Throwable error) {
                dr.onFail(error.toString());
            }
        });
    }

    /**
     * 获取所有机型
     *
     * @param dr
     */
    public static void geTallGoods(final Context mContext, final DataResponse dr) {
        RequestParams params = new RequestParams();
        params.put("isIos",0);
        
        MyHttp.get(BASE_URL + GOODS, params, new TextHandler() {
            @Override
            public void onSuccess(int header, String response) {

                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(response);
                    int status = jsonObject.getInt("stats");
                    if (status == 1) {

                        dr.onSucc(response);

                        SharePrefUtil.getInstance(mContext).putString(SpType.KEY_PHONE_GOOGS, response);
                    } else {
                        dr.onFail("获取失败");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }

            @Override
            public void onFailure(Throwable error) {
                dr.onFail("获取失败");
            }
        });
    }

    /**
     * @param dr
     * @param attrString //提交的报价记录
     */
    public static void saveAttr(Context mContext, String attrString, String imie, boolean isAPP, String iszy,final DataResponse dr) {
        RequestParams params = new RequestParams();

        params.put("pid", SharePrefUtil.getInstance(mContext).getString(KEY_PHONE_PID));
        params.put("bjgz", SharePrefUtil.getInstance(mContext).getString(KEY_BJGZ));
        params.put("parms", attrString);
        params.put("imei", imie);
        params.put("isAPP", isAPP ? "2" : "1");
        params.put("iszy", iszy);

        MyHttp.post(BASE_URL + SAVEATTR, params, new TextHandler() {
            @Override
            public void onSuccess(int header, String response) {

                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(response);
                    int status = jsonObject.getInt("stats");
                    if (status == 1) {

                        String data = jsonObject.getString("data");
                        dr.onSucc(data);

                    } else {

                        dr.onFail(jsonObject.getString("msg"));

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Throwable error) {
                dr.onFail("获取失败");
            }
        });
    }
    /**
     * @param dr
     *
     */
    public static void GetNewRecoverBasket(Context mContext, String productName,final DataResponse dr) {
        RequestParams params = new RequestParams();
        params.put("productName",productName);

        MyHttp.get(GetNewRecoverBasket, params, new TextHandler() {
            @Override
            public void onSuccess(int i, String response) {
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(response);
                    int status = jsonObject.getInt("code");
                    if (status == 0) {

                        JSONArray data = jsonObject.getJSONArray("data");

                        if (data != null && data.length() > 0) {
                            ArrayList<RecoverBasketEntity> recoverBasketEntities = new ArrayList<>();
                            for (int j = 0; j<data.length();j++) {
                                String o = (String) data.get(j).toString();
                                RecoverBasketEntity recoverBasketEntity = new Gson().fromJson(o, RecoverBasketEntity.class);
                                recoverBasketEntities.add(recoverBasketEntity);
                            }

                            dr.onSucc(recoverBasketEntities);
                        }



                    } else {

                        dr.onFail(jsonObject.getString("msg"));

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Throwable throwable) {

            }
        });
    }


}
